package com.example.platosreports;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;

public class Daily extends DailySearch {

@Override
protected void onCreate(Bundle savedInstanceState) 
{
		super.onCreate(savedInstanceState);
		
		//Get the date from intent
		Intent intent = getIntent();
		String date = intent.getStringExtra(DailySearch.THIS_DATE);
		
		setContentView(R.layout.activity_daily);
		
		try {
			dailyInput(date);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//Up Action on Action Bar
		setupActionBar();
}


//Function that searches text file and prints out data to screen
	private void dailyInput(String date) throws IOException
	{
		String fileName = "S" + date + ".txt";
		File inFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(), fileName);
		
		//Variables to store data in
		String sales = null;
		String buys = null;
		String cashDeposit = null;
		String checkDeposit = null;
		String ccDeposit = null;
		String itemsBought = null;
		String avgTrans = null;
		String returns = null;
		String mtd = null;
		String ytd = null;

		if(inFile.exists())
		{
			FileInputStream tryThis = new FileInputStream(inFile);
			BufferedReader myReader = new BufferedReader(new InputStreamReader(tryThis));
			
			int count = 1;  //counter to get to needed line
			String data2 = null;  //hold input string
			data2 = myReader.readLine();
			String [] inputArr = data2.split(" +");
			int flag = 1;
			
			TextView testData2 = (TextView) findViewById(R.id.title);
			testData2.setText("Daily Numbers");
			
			
			//Gets Cash Deposit
			while(flag != -999)
			{	
				data2 = myReader.readLine();
				inputArr = data2.split(" +");
				
				if(inputArr[0].equals("-------------------------"))
				{
					if(inputArr[5].equals("Cash"))
					{
						if(inputArr[6].equals("Deposit"))
						{
							cashDeposit = inputArr[7];
							flag = -999;
						}
						else
						{
							flag = 1;
						}
					}
					else 
					{
						flag = 1;
					}
				}		
				else
				{
					flag = 1;
				}
			}
			
			//Gets CC Deposit
			flag = 1;
			while(flag != -999)
			{	
				data2 = myReader.readLine();
				inputArr = data2.split(" +");
				
				if(inputArr[0].equals("========================="))
				{
					if(inputArr[4].equals("Card"))
					{
						if(inputArr[5].equals("Deposit"))
						{
							ccDeposit = inputArr[6];
							flag = -999;
						}
						else
						{
							flag = 1;
						}
					}
					else 
					{
						flag = 1;
					}
				}		
				else
				{
					flag = 1;
				}
			}
			
			//Gets Net Sales and Buy Total
			flag = 1;
			while(flag != -999)
			{	
				data2 = myReader.readLine();
				inputArr = data2.split(" +");
				
				if(inputArr[0].equals("Total"))
				{
					if(inputArr[1].equals("Net"))
					{
						if(inputArr[4].equals("|"))
						{
							sales = inputArr[3];
							buys = inputArr[8];
							flag = -999;
						}
						else
						{
							flag = 1;
						}
					}
					else 
					{
						flag = 1;
					}
				}		
				else
				{
					flag = 1;
				}
			}
			
			//Find Number of Items Bought
			flag = 1;
			while(flag != -999)
			{	
				data2 = myReader.readLine();
				inputArr = data2.split(" +");
					
				if(inputArr[0].equals("USED"))
				{
					if(inputArr[1].equals("TOTAL"))
					{
						itemsBought = inputArr[2];
						flag = -999;
					}
					else 
					{
						flag = 1;
					}
				}		
				else
				{
					flag = 1;
				}
			}
			
			//Find Average $ per Trans.
			flag = 1;
			while(flag != -999)
			{	
				data2 = myReader.readLine();
				inputArr = data2.split(" +");
					
				if(inputArr[0].equals("Average"))
				{
					if(inputArr[1].equals("Trans."))
					{
						avgTrans = inputArr[2];
						flag = -999;
					}
					else 
					{
						flag = 1;
					}
				}		
				else
				{
					flag = 1;
				}
			}
			
			//$ amount of returns
			flag = 1;
			while(flag != -999)
			{	
				data2 = myReader.readLine();
				inputArr = data2.split(" +");
					
				if(inputArr[0].equals("02"))
				{
					if(inputArr[1].equals("USED"))
					{
						returns = inputArr[3];
						flag = -999;
					}
					else 
					{
						flag = 1;
					}
				}		
				else
				{
					flag = 1;
				}
			}
			
			//Gets mtd and ytd sales
			flag = 1;
			while(flag != -999)
			{
				data2 = myReader.readLine();
				
				inputArr = data2.split(" +");	
				if(inputArr[0].equals("04"))
				{
					data2 = myReader.readLine();
					data2 = myReader.readLine();
					inputArr = data2.split(" +");
					mtd = inputArr[2];
					ytd = inputArr[3];
					flag = -999;
				}
				else
				{
					flag = 1;
				}
			}
			
//Display output to screen			
			TextView dissales = (TextView) findViewById(R.id.textView2);
			dissales.setText(sales);
			TextView disbuys = (TextView) findViewById(R.id.textView3);
			disbuys.setText(buys);
			TextView discashDeposit = (TextView) findViewById(R.id.textView4);
			discashDeposit.setText(cashDeposit);
			TextView disccDeposit = (TextView) findViewById(R.id.textView5);
			disccDeposit.setText(ccDeposit);
			TextView disitemsBought = (TextView) findViewById(R.id.textView6);
			disitemsBought.setText(itemsBought);
			TextView disavgTran = (TextView) findViewById(R.id.textView7);
			disavgTran.setText(avgTrans);
			TextView disreturns = (TextView) findViewById(R.id.textView8);
			disreturns.setText(returns);
			TextView dismtd = (TextView) findViewById(R.id.textView9);
			dismtd.setText(mtd);
			TextView disytd = (TextView) findViewById(R.id.textView10);
			disytd.setText(ytd);
			
		}
		else
		{
			TextView testData2 = (TextView) findViewById(R.id.fileNotFound);
			testData2.setText("File Not Found");
		}
		
	
	}
	
	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.daily, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			Intent intent1 = new Intent(this, PlatosReports.class);
			intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent1);
			
		}
		return super.onOptionsItemSelected(item);
	}

}
